package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.CheckDao;
import dao.impl.ICheckDao;
import entity.User;

public class CheckServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		User u = new User();
		CheckDao cd = new ICheckDao();
		
			String msg = "<response>";
			
			String sname = request.getParameter("sname");
			String fname = request.getParameter("fname");
			String lname= request.getParameter("lname");
			String pass= request.getParameter("pass");
			String email= request.getParameter("email");
			String sex= request.getParameter("sex");
			String nyear= request.getParameter("nyear");
			String nmonth= request.getParameter("nmonth");
			String nday= request.getParameter("nday");
			String born = nyear+"-"+nmonth+"-"+nday;
			
			u.setUsername(sname);
			u.setPassword(pass);
			u.setBorn(born);
			u.setName(lname+fname);
			u.setEmail(email);
			u.setSex(sex);
			u.setPower("1");//默认买家
			
			boolean flag =cd.adduser(u);
			if (flag) {
				msg+="<result>success</result>";
				request.getSession().setAttribute("username", sname);
			}
			msg+="</response>";
		response.setContentType("text/xml;charset=UTF-8");
		//设置响应对象中的缓存是否启用
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		out.println(msg);
		out.close();
	}

}
